<template>
  <div class="app-container">
    <el-form ref="formRef" :model="form" label-width="110px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="出入仓类型" prop="inOrderType">
            <dict-tag :options="inout_storehouse" :value="form.inOrderType" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="入仓单编号" prop="inOrderCode">
            <el-tooltip effect="dark" :content="form.inOrderCode">
              <div class="form-text">{{ form.inOrderCode }}</div>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="入仓单名称" prop="inOrderName">
            <el-tooltip effect="dark" :content="form.inOrderName">
              <div class="form-text">{{ form.inOrderName }}</div>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="ERP出库单号" prop="erpOrderCode">
            <el-tooltip effect="dark" :content="form.erpOrderCode">
              <div class="form-text">{{ form.erpOrderCode }}</div>
            </el-tooltip>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="工厂" prop="factoryCode">
            <div class="form-text">{{ form.factoryName }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工段" prop="workshopCode">
            <div class="form-text">{{ form.workShopName }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="产线" prop="lineCode">
            <div class="form-text">{{ form.lineName }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工艺段" prop="sectionCode">
            <div class="form-text">{{ form.sectionName }}</div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="送料人" prop="deliveryPerson">
            <el-tooltip effect="dark" :content="form.deliveryPerson">
              <div class="form-text">{{ form.deliveryPerson }}</div>
            </el-tooltip>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="来料时间" prop="comeTime">
            <el-tooltip effect="dark" :content="form.comeTime">
              <div class="form-text">{{ form.comeTime }}</div>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="入仓时间" prop="inTime">
            <el-tooltip effect="dark" :content="form.inTime">
              <div class="form-text">{{ form.inTime }}</div>
            </el-tooltip>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="备注" prop="remark">
            <div class="form-text">{{ form.remark }}</div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-tabs
      v-hasPermi="['warehouse:WhStorehouseInDetail:list']"
      v-model="activeName"
      class="demo-tabs"
    >
      <el-tab-pane label="入仓明细" name="first">
        <el-form
          :model="queryParams"
          ref="queryRef"
          :inline="true"
          label-width="100px"
          class="form-search"
        >
          <el-form-item label="ERP订单编号" prop="orderCode">
            <el-input
              v-model="queryParams.orderCode"
              placeholder="请输入ERP订单编号"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="产品名称" prop="productName">
            <el-input
              v-model="queryParams.productName"
              placeholder="请输入产品名称"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="物料名称" prop="matName">
            <el-input
              v-model="queryParams.matName"
              placeholder="请输入物料名称"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="search" @click="handleQuery">查询</el-button>
            <el-button icon="refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-form ref="tableForm" :model="whStorehouseInDetailList" label-width="0">
          <el-table
            :data="whStorehouseInDetailList"
            :row-class-name="rowWhStorehouseInDetailIndex"
            border
            maxHeight="400"
            ref="whStorehouseInDetail"
          >
            <el-table-column label="序号" align="center" prop="index" width="80" />
            <el-table-column label="ERP订单编号" prop="orderCode" width="150" />
            <el-table-column label="产品名称" prop="productName" width="150" />
            <el-table-column label="产品编码" prop="productCode" width="150"></el-table-column>

            <el-table-column label="物料编号" prop="matCode" width="150" />

            <el-table-column label="物料名称" prop="matName" width="150" />
            <el-table-column label="来料数量" prop="receiveNumber" width="150" />

            <el-table-column label="单位" prop="unit" width="150" />
            <el-table-column label="规格" prop="spec" width="150" />
            <el-table-column label="批次号" prop="batchNo" width="150" />
            <el-table-column label="数量" prop="receiveNumber" width="150" />

            <el-table-column label="线边仓" prop="storehouseName" width="150"></el-table-column>
            <el-table-column label="库位" prop="binName" width="150" />
          </el-table>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script setup name="warehouse">
import { getInStorehouse } from "@/api/warehouse/inStorehouse";
import { listWhStorehouseInDetail } from "@/api/warehouse/WhStorehouseInDetail";
import { reactive, toRefs, useAttrs, getCurrentInstance, ref, onMounted } from "vue";
const attrs = useAttrs();
const { proxy } = getCurrentInstance();
const activeName = ref("first");
const { inout_storehouse } = proxy.useDict("inout_storehouse");
const operationTitle = ref("");
const isBatch = ref(false);
const refData = reactive({
  // 表单参数
  form: {},
  queryParams: {
    batchNo: "",
    matName: "",
    matCode: "",
    matTypeName: "",
  },
  // 表单校验
});
const { form, queryParams } = toRefs(refData);
const emit = defineEmits(["close", "setActions", "setIsActionTop"]);
const whStorehouseInDetailList = ref([]);
const formRef = ref("formRef");
const tableForm = ref("tableForm");
const allTableData = ref([]);
const binEditAble = ref(false);
const actions = ref([]);

/** 搜索按钮操作 */
function handleQuery() {
  filterSubTable();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  filterSubTable();
}
function filterSubTable() {
  const result = [];
  allTableData.value.forEach((el) => {
    if (
      ((queryParams.value.orderCode && el.orderCode === queryParams.value.orderCode) ||
        !queryParams.value.orderCode) &&
      ((queryParams.value.productName && el.productName.includes(queryParams.value.productName)) ||
        !queryParams.value.productName) &&
      ((queryParams.value.matName && el.matName.includes(queryParams.value.matName)) ||
        !queryParams.value.matName)
    ) {
      result.push(el);
    }
  });
  whStorehouseInDetailList.value = result;
}

// 表单重置
function reset() {
  form.value = {
    id: "",
    inOrderType: "0",
    inOrderCode: "",
    inOrderName: "",
    erpOrderCode: "",
    factoryCode: "",
    workshopCode: "",
    lineCode: "",
    sectionCode: "",
    storehouseCode: "",
    deliveryPerson: "",
    receiver: "",
    comeTime: "",
    inTime: "",
    status: "",
    remark: "",
    whStorehouseInDetailList: [],
  };
  formRef.value.resetFields();
  whStorehouseInDetailList.value = [];
  allTableData.value = [];
}

/** 修改按钮操作 */
function getDetail(id) {
  getInStorehouse(id).then((response) => {
    form.value = response.data;
    if (proxy.checkPermi(["warehouse:WhStorehouseInDetail:list"])) {
      getListWhStorehouseInDetail(response.data.inOrderCode);
    }
  });
}

/** 物料入仓明细序号 */
function rowWhStorehouseInDetailIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

function getListWhStorehouseInDetail(code) {
  listWhStorehouseInDetail({ pageSize: 10000, pageNum: 1, storehouseInCode: code }).then((res) => {
    if (isBatch.value) {
      whStorehouseInDetailList.value.push(...res.rows);
      allTableData.value.push(...res.rows);
    } else {
      whStorehouseInDetailList.value = res.rows;
      allTableData.value = res.rows;
    }
  });
}

onMounted(() => {
  reset();
  const { data, title } = attrs;
  binEditAble.value = true;
  operationTitle.value = title;
  emit("setActions", actions.value);
  if (data) {
    getDetail(data.mainTableId);
  }
});
</script>

<style lang="scss">
.form-text {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
